package view
{
	import com.liu.load.LoadInfo;
	import com.liu.load.LoadManager;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.geom.ColorTransform;
	import flash.net.URLRequest;
	
	import models.ModelLocator;
	
	public class MovieSprite extends Sprite
	{
		public var content:Sprite;
		public var data:Object;
		private var model:ModelLocator = ModelLocator.getInstance();
		public function MovieSprite()
		{
			content = new Sprite;
			this.addChild(content);
		}
		public function setData(obj:Object):void{
			this.data = obj;
			this.x = obj.x;
			this.y = obj.y;
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
			var str:String = data.url;
			if(str.indexOf("http://") != -1){
				loader.load(new URLRequest(data.url));
			}else{
				loader.load(new URLRequest("http://www.ugan.cn/flash/bin-debug/" + data.url));
			}
			
		}
		public function onComplete(event:Event):void{
			var cls:Class = Class(event.target.applicationDomain.getDefinition("icon"));
			
			var movie:MovieClip = event.target.content// new cls();
			movie.graphics.beginFill(0xff0000,0);
			movie.graphics.drawRect(0,0,movie.width,movie.height);
			movie.graphics.endFill();
			
			if(data.hasOwnProperty("edit") && data.edit == "1")
				setTagColor(movie,data.color);
			movie.scaleX = movie.scaleY = data.scale;
			content.addChild(movie);
			movie.x = -movie.width/2;
			movie.y = -movie.height/2;
			content.rotation = data.rotation;
			
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		private function setTagColor(movie:MovieClip,colorStr:String):void{
			var colorAry:Array = colorStr.split(",");
			for(var i:int=0;i<colorAry.length;i++){
				var str:String = "0x" + colorAry[i];
				colorAry[i] = parseInt(str, 16);
			}
			for(i=0;i<colorAry.length;i++){
				var color:uint = colorAry[i];
				var blue:int = color & 0xFF;
				var green:int = (color >> 8) & 0xFF;
				var red:int = (color >> 16) & 0xFF;
				var colorT:ColorTransform = new ColorTransform(0,0,0,1,red,green,blue,0);
				if(i == 0){
					if(movie.first){
						MovieClip(movie.first).transform.colorTransform = colorT;
					}else{
						MovieClip(movie).transform.colorTransform = colorT;
					}
				}else if(i == 1){
					if(movie.second){
						MovieClip(movie.second).transform.colorTransform = colorT;
					}
				}else if(i == 2){
					if(movie.third){
						MovieClip(movie.third).transform.colorTransform = colorT;
					}
				}
			}
			
		}
	}
}